/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simfinal;

import com.jme.app.SimpleGame;
import com.jme.scene.shape.Sphere;
import com.jme.bounding.BoundingBox;
import com.jme.renderer.ColorRGBA;
import com.jme.math.Vector3f;
import com.jme.scene.state.LightState;
import com.jme.scene.TriMesh;
import com.jme.scene.Controller;
import com.jme.math.Quaternion;
import com.jme.math.FastMath;



/**
 *
 * @author jkeister
 */
public class TestGravity extends SimpleGame
{
    
    Sphere planet1 = null;
    Sphere planet2 = null;
    Sphere planet3 = null;
    Sphere planet4 = null;
    
    Particle [] planets = null;
    Nbody e0 =  null;
    Vector p0 = null;
    Vector p1 = null;
    Vector p2 = null;
    Vector p3 = null;
    
    Rocket r1 = null;
    
    public TestGravity()
    {
    }

    public static void main(String[] args)
    {
        TestGravity app = new TestGravity(); // Create Object
        // Signal to show properties dialog
        app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);
        app.start(); // Start the program
    }
    
	protected void simpleInitGame()
        {
		planet1 = new Sphere("planet1",10,10,0.1f);
                planet2 = new Sphere("planet2",10,10,1);
		planet3 = new Sphere("planet3",10,10,0.2f);
		planet4 = new Sphere("planet4",10,10,.05f);
		//Sphere planet5=new Sphere("planet5",20,20,50);
		//System.out.println(planet1.getCenter());
                
                
		planet2.setSolidColor(ColorRGBA.blue);
		planet3.setSolidColor(ColorRGBA.red);
		planet4.setSolidColor(ColorRGBA.orange);
		//planet5.setSolidColor(ColorRGBA.yellow);
		
             
		planet1.setLocalTranslation(new Vector3f(100, 10, 0));
		planet2.setLocalTranslation(new Vector3f(0, 0, 0));
		planet3.setLocalTranslation(new Vector3f(10, 10, 0));
		planet4.setLocalTranslation(new Vector3f(10, 10, 0));
		//planet5.setLocalTranslation(new Vector3f(50, 50, 0));
		
		// Do bounds for the sphere, but we'll use a BoundingBox this time
		planet1.setModelBound(new BoundingBox());
		planet2.setModelBound(new BoundingBox());
		planet3.setModelBound(new BoundingBox());
		planet4.setModelBound(new BoundingBox());
		//planet5.setModelBound(new BoundingBox(new Vector3f(1000,1000,1000),1000,1000,1000));
                
		planet1.updateModelBound();
		planet2.updateModelBound();
		planet3.updateModelBound();
		planet4.updateModelBound();
		//planet5.updateModelBound();
		// Give the sphere random colors
		
                
                		
		Sphere s=new Sphere("My sphere",10,10,.1f);
		// Do bounds for the sphere, but we'll use a BoundingBox this time
		s.setModelBound(new BoundingBox(new Vector3f(10,10,10),10,10,10));
		s.updateModelBound();
		// Give the sphere random colors
		//s.setRandomColors();
		
		planet1.setSolidColor(ColorRGBA.green);
			 
		rootNode.attachChild(planet1); // Put it in the scene graph
		rootNode.attachChild(planet2); // Put it in the scene graph
		rootNode.attachChild(planet3); // Put it in the scene graph
                rootNode.attachChild(planet4); // Put it in the scene graph
                //rootNode.attachChild(planet5); // Put it in the scene graph

                rootNode.setLightCombineMode(LightState.OFF);
                rootNode.attachChild(s); // Put it in the scene graph

			 
		
		s.addController(new PlanetRotator2(s));
                
                planets = new Particle[4];
                
                planets[0] = new Particle(7.5, new Vector(0.0,0.0,0.0), new Vector(0.0,0.0,0.0));
                planets[1] = new Particle(0.00001, new Vector(10,0,0.0), new Vector(0,0.5,0));
                planets[2] = new Particle(.00001, new Vector(15,0,0), new Vector(0,0.5,0));

                //planets[3] = new Particle();
                //planets[3].set(0.00001, new Vector(1.0,0.0,0.0), new Vector(0.0,-1.0,0.0));
                                        
                        r1 = new Rocket("000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "000000000000000000000000000000000000000000000000000" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "111111111111111111111111111111111111111111111111111" +
                                "", new Vector(-10,1,-1));
                                
                        
                        planets[3] = r1;
                
                e0 = new Nbody(planets, 0.00001);
                
                p0 = new Vector(e0.system[0].x0);
                p1 = new Vector(e0.system[1].x0);
                p2 = new Vector(e0.system[2].x0);
                p3 = new Vector(e0.system[3].x0);        
    }
	
	protected void simpleRender()
	{
		
	}
	
	class PlanetRotator2 extends Controller
	{
		float degree;
		TriMesh shape;
		
		PlanetRotator2(TriMesh shape)
		{
			this.shape = shape;
			degree = 0;
		}

		public void update(float time)
		{
			Quaternion q1 = new Quaternion(); //shape.getLocalRotation().w+1, shape.getLocalRotation().x+1, shape.getLocalRotation().y+1, shape.getLocalRotation().z+1
		
			degree += 1f;
			q1.fromAngleAxis(FastMath.DEG_TO_RAD*degree, new Vector3f(0,1,0));
                                              
                        //e0.system[3].x0.add(r1.x0dot);
                        
                        e0.advance(0.01);                 
                        
                        r1.Fire();
                        
                        planet1.setLocalTranslation(e0.getPosition(1));
                        planet2.setLocalTranslation(e0.getPosition(0));
                        planet3.setLocalTranslation(e0.getPosition(2));
                        planet4.setLocalTranslation(e0.getPosition(3));

			shape.setLocalRotation(q1);             
		}
	}
    
    
    
}
